package cn.com.elite;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

public class ISBean {
	private static final Log log=LogFactory.getLog(ISBean.class);
	private DataSource dataSource;
    private DataSourceTransactionManager tran;
	public DataSource getDataSource() {
		return dataSource;
	}
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	public DataSourceTransactionManager getTran() {
		return tran;
	}
	public void setTran(DataSourceTransactionManager tran) {
		this.tran = tran;
	}
	public List chaxun_sql(String sql){
		List result=null;
		try{	
			log.debug("sql:"+sql);
			JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
			result=jdbcTemplate.queryForList(sql);
		}catch(Exception e){
			log.error("chaxun_sql_ERRO:"+Util.getERRO(e));
		}
		return result;
	}
	
	public int update_sql(String sql) throws Exception{
		int result=0;

		try{
			JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
			log.info("sql:"+sql);	
			result=jdbcTemplate.update(sql);
		}catch(Exception e){
			log.error("update_sql_ERRO:"+Util.getERRO(e));
			throw e;
		}
		log.info(sql+":"+result);
		return result;
	}
    public String chaxunString(String sql) throws Exception{   
    	log.info("sql:"+sql);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String resultList="";
        try{
        	resultList=(String)jdbcTemplate.queryForObject(sql,java.lang.String.class);
        }catch(Exception e){
        	log.info(sql+":"+Util.getERRO(e));
        	throw e;
        }
        return resultList;
   }
    
    public String dm_tpimport_bakelite(){
    	JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
        String param2Value=jdbcTemplate.execute(
            new CallableStatementCreator() {
                public CallableStatement createCallableStatement(Connection con){            
                    String storedProc="{call dm_tpimport_bakelite(?)}";// 调用的sql
                    CallableStatement cs=null;
                    try{
                    	cs=con.prepareCall(storedProc);
                   }catch(Exception e){
                	   log.info("cs = con.prepareCall(storedProc)错误:"+e);
                   }
                   try{
                   	   cs.setInt(1,0);// 设置输入参数的值
                   }catch(Exception e){
                	   log.info("设置输入参数的值错误:"+Util.getERRO(e));
                   }
                   try{
                	   cs.registerOutParameter(1,Types.INTEGER);// 注册输出参数的类型
                   }catch(Exception e){                	   
                	   log.info("注册输出参数的类型错误:"+Util.getERRO(e));
                   }
                    return cs;
                }
            }, new CallableStatementCallback() {
                public Object doInCallableStatement(CallableStatement cs)  {
                    try{
                    	cs.execute();
                    }catch(Exception e){
                    	log.info("cs.execute()错误:"+Util.getERRO(e));
                    }
                    try{
                    	   return cs.getInt(1);// 获取输出参数的值
                    }catch(Exception e){
                    	log.info("cs.getString(1):"+Util.getERRO(e));
                    	return null;
                    }
            }
        }).toString();
        log.info("dm_tpimport_bakelite_param2Value:"+param2Value);
        return param2Value;
    }
}
